<?php
namespace Home\Controller;

use Home\Util\lib\YZTokenClient;
use Util\AccessController;
use Home\Util\lib\YouZanApiFunction;

class APIGETController extends AccessController {

    /*
     * 由id获取书籍信息
     */
    public function GetAllBook() {
        $field = C('BOOK_LS');
        $where['SXJ'] = 1;
        $limit = GET_LIMIT($_GET['page'], $_GET['page_num']);
        $order = 'SJ_TIME desc';
        $LS = Service('API', 'book');
        $DATA = $LS->query($where, $limit, $field, $order);
        $res = $this->ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     * 根据书籍ID查询书籍的详情
     */
    public function GetBook_ID() {
        header("Access-Control-Allow-Origin:*");
        $where['ID'] = $_GET['ID'];
        $field = C('BOOK_FIELD');
        $LS = Service('API', 'book');
        $DATA = $LS->find($where, $field, 1);
        $res = $this->ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
    * 参数
    */
    public function GetPOST() {
        $this->ajaxReturn($_GET, 'JSON');
    }

    /*
     * 根据书籍条形码查询书籍的列表
     */
    public function GetBook_ISBN() {
        $where['ISBN'] = $_GET['ISBN'];
        $field = C('BOOK_LS');
        $LS = Service('API', 'book');
        $DATA = $LS->query($where, null, $field);
        $res = $this->ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
    * 根据书籍ID查询书籍的库存
    */
    public function GetBook_STOCK() {
        $where['ID'] = $_GET['ID'];
        $LS = Service('API', 'book');
        $DATA = $LS->find($where, 'STOCK');
        $res = $this->ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     * 模糊查询书籍列表
     */
    public function Search_Books() {
        $str = $_GET['STR'];
        $field = C('BOOK_LS');
        $strwhere = LIKEGET_MH($str);
        $where[_complex] = $strwhere;
        $_GET['age'] ? $where['AGE'] = array('like',
            '%' . $_GET['age'] . '%') : 1;
        $_GET['language'] ? $where['LANGUAGES'] = $_GET['language'] : 1;
        $_GET['back'] ? $where['PACKING_SPECIFICATION'] = $_GET['back'] : 1;
        //        dump($where);die();
        //综合排序（按库存）
        $_GET['all_sort'] == 1 ? $order = ' SORT desc ,SJ_TIME desc' : 1;

        //分页
        $limit = GET_LIMIT($_GET['page'], $_GET['page_num']);

        $LS = Service('API', 'book');
        $DATA = $LS->query($where, $limit, $field, $order);
        $res = $this->ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     *根据ID查询书籍的名字，封面，和价格（租借价格，购买价格）
     */
    public function GetBook_field_ID() {
        $where['ID'] = $_GET['ID'];
        //        $where['ID'] ='2331';
        $field = 'ID as book_id ,
         TITLE as book_name,
         PIC_URL as book_img ,
         Y_PRICE as book_old_money,
         XS_PRICE as book_pay_money';
        $LS = Service('API', 'book');
        $DATA = $LS->find($where, $field);
        $res = $this->ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     *根据书籍ID改变书籍本数（库存）
     */
    public function Update_STOCK_ID() {
        $where['ID'] = $_GET['ID'];
        $NUM = $_GET['NUM'];
        //        $where['ID'] ='2331';
        //        $NUM = 1;
        $LS = Service('API', 'book');
        $BOOKDATA = $LS->find($where);
        if ($BOOKDATA) {
            $save['STOCK'] = $BOOKDATA['STOCK'] + $NUM;
            if ($save['STOCK'] < 0) {
                $res = $this->ResErrorData(1);
            } else {
                $OK = $LS->update($where, $save);
                if ($OK) {
                    $DATA['STOCK'] = $save['STOCK'];
                    $res = $this->ResData($DATA);
                } else {
                    $res = $this->ResErrorData(2);
                }
            }
        } else {
            $res = $this->ResErrorData(3);
        }
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     *  智能选书的的筛选结果
     */
    /*
  *  智能选书的的筛选结果
  */
    public function GeZNtAllBook() {
        $field = C('BOOK_LS');
        if ($_GET['THEME']) {
            $where1['region_one'] = $_GET['THEME'];
            $where1['region_two'] = $_GET['THEME'];
            $where1['TITLE'] = $_GET['THEME'];
            $where1['KEY'] = $_GET['THEME'];
        }
        $_GET['AWARD'] ? $where1['KEY'] = $_GET['AWARD'] : 1;
        $where2 = LIKEGET($where1);
        if ($where2) {
            $where2['_logic'] = 'OR';
            $where[_complex] = $where2;
        }
        $where['SXJ'] = 1;
        $_GET['AUTHOR'] ? $where['AUTHOR'] = array('like',
            '%' . $_GET['AUTHOR'] . '%') : 1;
        $_GET['AGE'] ? $where['AGE'] = array('like',
            '%' . $_GET['AGE'] . '%') : 1;
        $_GET['PACK'] ? $where['PACKING_SPECIFICATION'] = $_GET['PACK'] : 1;

        if ($_GET['TOP'] == 1) {
            $LS = Service('API', 'book');
            $DATACOUNT = $LS->count($where);
            if (($_GET['PAGE']) * $_GET['PAGE_NUM'] < $DATACOUNT) {
                $randnum = rand(($_GET['PAGE']) * $_GET['PAGE_NUM'], $DATACOUNT);
            } else {
                $randnum = rand(0, ($_GET['PAGE'] - 1) * $_GET['PAGE_NUM']);
            }
            if ($randnum != 0) {
                $limit = GET_LIMIT($randnum, 1);
                $DATA = $LS->query($where, $limit, $field);
                $res = ResData($DATA);
            } else {
                $res = ResErrorData(4);
            }
        } else {
            $limit = GET_LIMIT($_GET['PAGE'], $_GET['PAGE_NUM']);
            $LS = Service('API', 'book');
            $DATA = $LS->query($where, $limit, $field);
            $res = ResData($DATA);
        }
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     * 书籍的分类 （第一级分类）
     */
    public function GetBookOneClass() {
        $LS = Service('Book', 'region');
        $flpdata = $LS->find(array('region_name' => '童书分类'));
        $yjfldata = $LS->query(array('region_fid' => $flpdata['region_id']), null, 'region_id,region_fid,region_name');
        $res = $this->ResData($yjfldata);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
    * 书籍的分类 （其他等级分类）
    */
    public function GetBookOthersClass() {
        $region_id = $_GET['ID'];
        //        $region_id = 'ZD59dd8b30269da2';
        $LS = Service('Book', 'region');
        $data = $LS->query(array('region_fid' => $region_id));
        $res = $this->ResData($data);
        $this->ajaxReturn($res, 'JSON');
    }


    /*
     * 主页书籍排序需要分页
     */
    public function SelectlabelBook() {
        $field = C('BOOK_LS');
        $_GET['AGE'] ? $where = LIKEGET(array('AGE' => $_GET['AGE'])) : 1;
        $_GET['TYPE'] ? $where['region_one'] = $_GET['TYPE'] : 1;
        $_GET['LANGUAGES'] ? $where['LANGUAGES'] = $_GET['LANGUAGES'] : 1;
        $where['SXJ'] = 1;

        $_GET['SORT'] ? $order = $_GET['SORT'] : 1;

        $limit = GET_LIMIT($_GET['PAGE'], $_GET['PAGE_NUM']);
        $LS = Service('API', 'book');
        $data = $LS->query($where, $limit, $field, $order);
        $res = $this->ResData($data);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     * 数据返回格式
     */
    public function ResData($DATA) {
        if ($DATA) {
            $res['datas'] = $DATA;
            $res['msg'] = 200;
            $res['desc'] = 'success';
            $res['res_num'] = count($DATA);
        } else {
            $res['msg'] = -1;
            $res['desc'] = 'error';
        }
        return $res;
    }

    /*
     * 返回错误的信息
     * 参数 1 2 3 4 5
     * 返回不同错误值
     */
    public function ResErrorData($top) {
        $str = "数据错误";
        $str = $top == 1 ? "书籍仓库不足" : $str;
        $str = $top == 2 ? "数据修改失败" : $str;
        $str = $top == 3 ? "无对应书籍信息/书籍ID有误" : $str;

        $res['msg'] = -1;
        $res['desc'] = $str;
        return $res;
    }


    public function ZNTEST() {
        $field = C('BOOK_LS');
        if ($_GET['THEME']) {
            $where1['region_one'] = $_GET['THEME'];
            $where1['region_two'] = $_GET['THEME'];
            $where1['TITLE'] = $_GET['THEME'];
            $where1['KEY'] = $_GET['THEME'];
        }
        $_GET['AWARD'] ? $where1['KEY'] = $_GET['AWARD'] : 1;
        $where2 = LIKEGET($where1);
        if ($where2) {
            $where2['_logic'] = 'OR';
            $where[_complex] = $where2;
        }
        $where['SXJ'] = 1;
        $_GET['AUTHOR'] ? $where['AUTHOR'] = array('like',
            '%' . $_GET['AUTHOR'] . '%') : 1;
        $_GET['AGE'] ? $where['AGE'] = array('like',
            '%' . $_GET['AGE'] . '%') : 1;
        $_GET['PACK'] ? $where['PACKING_SPECIFICATION'] = $_GET['PACK'] : 1;

        if ($_GET['TOP'] == 1) {
            $LS = Service('API', 'book');
            $DATACOUNT = $LS->count($where);
            if (($_GET['PAGE']) * $_GET['PAGE_NUM'] < $DATACOUNT) {
                $randnum = rand(($_GET['PAGE']) * $_GET['PAGE_NUM'], $DATACOUNT);
            } else {
                $randnum = rand(0, ($_GET['PAGE'] - 1) * $_GET['PAGE_NUM']);
            }
            $limit = GET_LIMIT($randnum, 1);
            dump($randnum);
            die();
            $DATA = $LS->query($where, $limit, $field);
        } else {
            $limit = GET_LIMIT($_GET['PAGE'], $_GET['PAGE_NUM']);
            $LS = Service('API', 'book');
            $DATA = $LS->query($where, $limit, $field);
        }


        $res = $this->ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

}